<template>
  <el-dialog
    title="分配角色"
    :visible="isAssign"
    @close="hClose"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="hClose">取 消</el-button>
      <el-button type="primary" @click="primary">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getAssignList } from '@/api/role'
import { updateAssign } from '@/api/permission'
import { Message } from 'element-ui'
import { getDetails } from '@/api/employee'
export default {
  props: {
    isAssign: {
      type: Boolean,
      required: true
    },
    currId: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.getAssignList()
  },
  methods: {
    // 获取员工详情
    async getEmployee() {
      const res = await getDetails(this.currId)
      // console.log(res.data.roleIds)
      // 数据回显
      this.roleIds = res.data.roleIds
    },
    // 获取已启用角色列表
    async getAssignList() {
      const res = await getAssignList()
      //   console.log(res)
      this.list = res.data
    },
    // 确认
    async primary() {
      await updateAssign({ roleIds: this.roleIds, id: this.currId })
      // 提示用户
      Message.success('分配角色成功')
      // 关闭弹窗
      this.hClose()
    },
    // 取消
    hClose() {
      this.$emit('update:isAssign', false)
    }
  }
}
</script>

<style>

</style>
